﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace Zombie_Demo.World
{
    public class Camera
    {
        protected float zoom;
        public Matrix transform;
        public Vector2 pos;
        protected float rotation;
        private float mouseWheelLast;
        private Rectangle boundingBox;

        public Rectangle BoundingBox
        {
            get { return this.boundingBox; }
        }

        public float Zoom
        {
            get { return zoom; }
            set { zoom = value; if (zoom < 0.1f) zoom = 0.1f; }
        }

        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }

        public Vector2 Position
        {
            get { return pos; }
            set { this.pos = value; }
        }

        public Camera(GraphicsDevice graphics)
        {
            zoom = 1.0f;
            rotation = 0.0f;
            pos = Vector2.Zero;
            mouseWheelLast = Mouse.GetState().ScrollWheelValue;

            this.boundingBox = new Rectangle(0, 0, graphics.Viewport.Width, graphics.Viewport.Height);
        }

        public Matrix getTransform(GraphicsDevice graphics)
        {
            transform =
                Matrix.CreateTranslation(new Vector3(-pos.X, -pos.Y, 0)) *
                Matrix.CreateRotationZ(Rotation) *
                Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
                Matrix.CreateTranslation(new Vector3(graphics.Viewport.Width * 0.5f, graphics.Viewport.Height * 0.5f, 0));

            return transform;
        }

        /// <summary>
        /// Update the camera
        /// </summary>
        /// <param name="position">Position to set the camera to</param>
        /// <param name="graphics">thje current graphcis device</param>
        public void Update(Vector2 position, GraphicsDevice graphics)
        {
            this.pos = position;

            this.boundingBox = new Rectangle((int)position.X - graphics.Viewport.Width / 2,
                (int)position.Y - graphics.Viewport.Height / 2,
                graphics.Viewport.Width, graphics.Viewport.Height);
        }
    }
}
